package weaver.interfaces.holyview.duss.action;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;


/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-10-23
 * @Description: 文档发布/废止审批流程；流程提交后生成文档编号  文档发布/废止审批：
 * * 如果是新增文档时，该文档的版本号从A0开始，
 * * 如果是变更文档时，根据替换的文档的版本，自动往下延申；
 * * 版本规则为：文档自动生成版本，规则为：ABCD…..；A是A0,A1,A2,A3,A4,B0,B1…….,按此顺序排序
 * @ModifyLog:
 **/

public class WeaWdfbFzspAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        baseBean.writeLog(">>>>>WeaWdfbFzspAction>>>>requestid=" + requestid);
        rs.executeQuery("select glzx,dybbh,wj from " + tablename + " where requestid=?", requestid);
        rs.next();
        //管理属性
        String glzx = Util.null2String(rs.getString("glzx"));
        //对应版本号
        String dybbh = Util.null2String(rs.getString("dybbh"));
        //文件
        String wj = Util.null2String(rs.getString("wj"));
        switch (glzx) {
            case "0":
                updateBbh(tablename,requestid,"A0",wj); break;
            case "1":
                String bbh = getSerialNumber(dybbh);
                updateBbh(tablename,requestid,bbh,wj); break;
            default:
        }

        return Action.SUCCESS;

    }

    private static String getSerialNumber(String str) {
        String returnStr = "";
        String[] list = {"A0", "A1", "A2", "A3", "A4", "B0", "B1", "B2", "B3", "B4", "C0", "C1", "C2", "C3", "C4",
                "D0", "D1", "D2", "D3", "D4", "E0", "E1", "E2", "E3", "E4", "F0", "F1", "F2", "F3", "F4",
                "G0", "G1", "G2", "G3", "G4", "H0", "H1", "H2", "H3", "H4", "I0", "I1", "I2", "I3", "I4",
                "J0", "J1", "J2", "J3", "J4", "K0", "K1", "K2", "K3", "K4", "L0", "L1", "L2", "L3", "L4",
                "M0", "M1", "M2", "M3", "M4", "N0", "N1", "N2", "N3", "N4"};
        for (int i = 0; i < list.length; i++) {
            if (str.equals(list[i])) {
                returnStr = list[i + 1];
            }
        }
        return returnStr;
    }

    private static void updateBbh(String tablename, String requestid, String bbh, String docid) {
        RecordSet rs = new RecordSet();
        rs.executeUpdate("update " + tablename + " set bb=? where requestid=?", bbh, requestid);
        rs.executeUpdate("update cus_fielddata set wjbb1=? where scope='DocCustomFieldBySecCategory' and id=?", bbh, docid);
    }

}
